define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/courier/index' + location.search,
                    add_url: 'order/courier/add',
                    // edit_url: 'order/courier/edit',
                    multi_url: 'order/courier/multi',
                    import_url: 'order/courier/import',
                    table: 'courier',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'order_pay_sn', title: __('Order_pay_sn'), operate: 'LIKE'},
                        {field: 'waybill', title: __('Waybill'), operate: 'LIKE'},
                        {field: 'filterStatus', title: __('FilterStatus'), searchList: {"1": __('filterStatus 1'),"2": __('filterStatus 2'),"3": __('filterStatus 3')},formatter: Table.api.formatter.status},
                        {field: 'filterResult', title: __('Filterresult'),searchList:{"1":__('Filterresult 1'),"2":__('Filterresult 2'),"3":__('Filterresult 3')},formatter: Table.api.formatter.status},
                        {field: 'remark',
                            title: __('Remark'),
                            operate: 'LIKE',
                            formatter: function (value, row, index) {

                                if (value == 1)
                                {
                                    var vl = '收方超范围';
                                    return vl;
                                }else if (value == 2)
                                {
                                    var vl = '派方超范围';
                                    return vl;
                                }else if (value >= 3){
                                    var vl = '高峰地段';
                                    return vl;
                                }else{
                                    var vl = '正常发货';
                                    return vl;
                                }

                            }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'courierselect',
                                    text: '快递查询',
                                    // classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-magic',
                                    url: 'order/courier/courierselect',
                                    
                                },
                                {
                                    name: 'orderquery',
                                    text: '取消发货',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'order/courier/orderquery',
                                    confirm: '是否全部取消发货',
                                    visible: function(res)
                                    {
                                        if (res.filterStatus == 3)
                                        {
                                            return false
                                        }else if(res.filterStatus == 2){
                                            return false;
                                        }else{
                                            return true;
                                        }
                                    },
                                    error: function (data,res){
                                        Layer.alert('错误信息:' + res.msg);
                                        return false;
                                    }
                                },
                                // {
                                //     name: 'print',
                                //     text: '打印',
                                //     classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                //     icon: 'fa fa-magic',
                                //     url: 'order/courier/print',
                                //     success: function (data,res){
                                //         console.log(res.msg)
                                //         // 返回数据
                                //         Layer.alert(res.msg);
                                //     },
                                //     error: function (data, res){
                                //         console.log(res.msg)
                                //         // 返回数据
                                //         Layer.alert(res.msg);
                                //     }
                                // }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'order/courier/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'order/courier/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'order/courier/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        courierselect: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
